﻿using System;
using System.Collections.Generic;
using System.Text;
using hedefgrup.reports.renault.database;
using System.Data;

namespace hedefgrup.reports.common
{
    public class Attribute
    {
        public string Key { get; private set; }
        public string Value { get; internal set; }
        public string Culture { get; private set; }
        public Nullable<DateTime> TimeStamp { get; private set; }
        public Tree Owner;

        public static readonly string[] AttributeTableFieldList = { "UniqueID", "Key", "Culture", "Value", "TimeStamp" };

        internal Attribute(Tree owner, string key, string culture, string value)
            : this(owner, key, culture, value, null)
        {

        }

        internal Attribute(Tree owner, string key, string culture, string value, Nullable<DateTime> timeStamp)
        {
            Owner = owner;
            Key = key;
            Culture = culture;
            Value = value;
            TimeStamp = timeStamp;
        }

        internal static SortedList<string, Attribute> GetAttributes(Tree owner)
        {
            DataTable attributeData = db.query("SELECT * FROM Attribute WHERE UniqueID = @p_0", owner.UniqueID);
            SortedList<string, Attribute> attributes = new SortedList<string, Attribute>();
            foreach (DataRow attributeRow in attributeData.Rows)
            {
                string key = attributeRow["Key"].ToString();
                string culture = attributeRow["Culture"].ToString();
                string value = attributeRow["Value"].ToString();
                DateTime timeStamp = Convert.ToDateTime(attributeRow["TimeStamp"]);
                if (!attributes.ContainsKey(key + "_" + culture))
                {
                    attributes.Add(key + "_" + culture, new Attribute(owner, key, culture, value, timeStamp));
                }
            }
            return attributes;
        }

        public void Persist()
        {
            if (Value == null)
            {
                string deleteStatement = "DELETE [Attribute] WHERE [UniqueID] = @p_0 AND [Key] = @p_1 AND [Culture] = @p_2";
                db.execute(deleteStatement, Owner.UniqueID, Key, Culture);
            }
            else
            {
                if (TimeStamp == null)
                {
                    string insertStatement = db.prepare_insert("Attribute", AttributeTableFieldList);
                    db.execute(insertStatement, Owner.UniqueID, Key, Culture, Value, TimeStamp = DateTime.Now);
                }
                else
                {
                    string updateStatement = "UPDATE [Attribute] SET Value = @p_0 WHERE [UniqueID] = @p_1 AND [Key] = @p_2 AND [Culture] = @p_3 AND [TimeStamp] = @p_4";
                    if (db.execute(updateStatement, Value, Owner.UniqueID, Key, Culture, TimeStamp.HasValue ? TimeStamp.Value : DateTime.Now) == 0)
                    {
                        throw new TreeConcurrencyException(Owner);
                    }
                }
            }
        }
    }
}
